package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FileMapper;
import com.itheima.dao.PictureMapper;
import com.itheima.dao.PictureSortMapper;
import com.itheima.pojo.File;
import com.itheima.pojo.Picture;
import com.itheima.pojo.PictureSort;
import com.itheima.service.PictureSortService;
import com.itheima.utils.IdWorker;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

@Service
public class PictureSortServiceImpl implements PictureSortService {

    @Resource
    private PictureSortMapper pictureSortMapper;

    @Resource
    private FileMapper fileMapper;

    @Resource
    private IdWorker idWorker;

    @Resource
    private PictureMapper pictureMapper;

    /**
     * 增加图片分类
     * @Param Picture
     * */
    @Override
    public Integer add(PictureSort pictureSort) {
        PictureSort pictureSort1 = new PictureSort();
        pictureSort1.setName(pictureSort.getName());
        PictureSort pictureSort2 = pictureSortMapper.selectOne(pictureSort1);
        if (pictureSort2 != null){
            return 0;
        }
        pictureSort.setUid(String.valueOf(idWorker.nextId()));
        pictureSort.setCreateTime(new Date());
        pictureSort.setUpdateTime(new Date());
        return pictureSortMapper.insertSelective(pictureSort);
    }

    /**
     * 删除图片分类
     * @Param Picture
     * */
    @Override
    @Transactional
    public Integer delete(PictureSort pictureSort) {
        Example example = new Example(Picture.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pictureSortUid",pictureSort.getUid());
        pictureMapper.deleteByExample(example);
        return pictureSortMapper.deleteByPrimaryKey(pictureSort);
    }

    /**
     * 编辑图片分类
     * @Param Picture
     * */
    @Override
    public Integer edit(PictureSort pictureSort) {
        pictureSort.setUpdateTime(new Date());
        return pictureSortMapper.updateByPrimaryKeySelective(pictureSort);
    }

    /**
     * 获取图片分类列表
     * @Param Picture
     * */
    @Override
    public Page<PictureSort> getList(PictureSort pictureSort) {

        Example example = new Example(PictureSort.class);
        Example.Criteria criteria = example.createCriteria();
        if (pictureSort.getKeyword() != null && !pictureSort.getKeyword().equals("")) {
            criteria.andLike("name", "%"+pictureSort.getKeyword()+"%");
        }

        PageHelper.startPage(pictureSort.getCurrentPage(),pictureSort.getPageSize());
        List<PictureSort> pictureSortList = pictureSortMapper.selectByExample(example);
        if(pictureSortList.size() > 0){
            for (PictureSort pictureSort1 : pictureSortList) {
                File file = fileMapper.selectByPrimaryKey(pictureSort1.getFileUid());
                List<String> photoList = new ArrayList<>();
                if(file != null) {
                    String picUrl = file.getPicUrl();
                    if(picUrl.startsWith("/blog")){
                        picUrl = "http://qf01xhkh2.hn-bkt.clouddn.com/"+picUrl;
                    }
                    photoList.add(picUrl);
                    pictureSort1.setPhotoList(photoList);
                }
            }
        }

        pictureSortList.sort(new Comparator<PictureSort>() {
            @Override
            public int compare(PictureSort p1, PictureSort p2) {
                return p1.getSort() < p2.getSort() ? 1 : -1;
            }
        });

        return (Page<PictureSort>)pictureSortList;
    }

    /**
     * 通过Uid获取分类
     * @Param Picture
     * */
    @Override
    public PictureSort getPictureSortByUid(PictureSort pictureSort) {
        PictureSort pictureSort1 = pictureSortMapper.selectByPrimaryKey(pictureSort.getUid());
        File file = fileMapper.selectByPrimaryKey(pictureSort1.getFileUid());
        List<String> photoList = new ArrayList<>();
        if(file != null) {
            String picUrl = file.getPicUrl();
            if(picUrl.startsWith("/blog")){
                picUrl = "http://qf01xhkh2.hn-bkt.clouddn.com/"+picUrl;
            }
            photoList.add(picUrl);
            pictureSort1.setPhotoList(photoList);
        }
        return pictureSort1;
    }

    /**
     * 置顶分类
     * @Param Picture
     * */
    @Override
    public Integer stick(PictureSort pictureSort) {
        List<PictureSort> pictureSorts = pictureSortMapper.selectAll();
        Integer count = 0;
        for (PictureSort sort : pictureSorts) {
            if(sort.getSort() > count){
                count =  sort.getSort();
            }
        }
        pictureSort.setSort(count+1);
        pictureSort.setUpdateTime(new Date());
        return pictureSortMapper.updateByPrimaryKeySelective(pictureSort);
    }


}
